package com.woniu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.woniu.entity.PageData;
import com.woniu.entity.dto.QueryMessageDTO;
import com.woniu.entity.vo.SystemMessageVO;
import com.woniu.mapper.MessageMapper;
import com.woniu.pojo.Message;
import com.woniu.pojo.User;
import com.woniu.service.IMessageService;
import com.woniu.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author xiaosong
 * @since 2023-04-01
 */
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message> implements IMessageService {
    @Autowired
    private IUserService userService;

    @Autowired
    private IMessageService messageService;

    @Autowired
    private MessageMapper messageMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public PageData select(Integer currentPage, Integer pageSize, QueryMessageDTO messageDTO) {
        System.out.println(messageDTO);
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(messageDTO.getUserName())) {
            wrapper.like("user_name", messageDTO.getUserName());
        }
        if (!StringUtils.isEmpty(messageDTO.getTitle())) {
            wrapper.like("title", messageDTO.getTitle());
        }
        if (!StringUtils.isEmpty(messageDTO.getStatus())) {
            wrapper.eq("status", messageDTO.getStatus());
        }
        if (!StringUtils.isEmpty(messageDTO.getType())) {
            wrapper.eq("type", messageDTO.getType());
        }
        if (!StringUtils.isEmpty(messageDTO.getCreateTimeStart())) {
            wrapper.ge("create_time", messageDTO.getCreateTimeStart());
        }
        if (!StringUtils.isEmpty(messageDTO.getCreateTimeEnd())) {
            wrapper.le("create_time", messageDTO.getCreateTimeEnd());
        }
        IPage<Message> page = new Page<>(currentPage, pageSize);
        IPage<Message> selectPage = messageMapper.selectPage(page, wrapper);
        PageData pageData = new PageData(selectPage.getTotal(), selectPage.getRecords());
        return pageData;
    }

    @Override
    public boolean add(Message message) {
        if (message.getType() == 2) {
            //当消息类型为系统消息时
            message.setUserName("全体用户");
            message.setUserId(0);
            message.setStatus((byte) 0);
            //设定这条系统消息的属性
            List<User> userList = userService.list();
            List<Integer> idList = new ArrayList<>();
            userList.forEach(s -> idList.add(s.getId()));
            //遍历出所有user对象 并且将user对象中的id存入idList集合中
            messageService.save(message);
            //保存这条消息进入数据库
            Integer messageId = message.getId();
            //取到这条消息的id
            redisTemplate.opsForSet().add("readable_message_"+messageId,idList.toArray());
            //在redis中创建一个以这条消息的id为key 以全体用户的id为value的set集合
        } else {
            User user = userService.getById(message.getUserId());
            message.setUserName(user.getName());
            messageService.save(message);
        }
        return true;
    }

    @Override
    public boolean updateMessage(Message message) {
        if (message.getType() == 2) {
            message.setUserName("全体用户");
            message.setUserId(0);
            message.setStatus((byte) 1);
            messageService.updateById(message);
        }else{
            User user = userService.getById(message.getUserId());
            message.setUserName(user.getName());
        }
        messageService.updateById(message);
        return true;
    }

    @Override
    public Integer getUnread(Integer id) {
        List<SystemMessageVO> messageVOList = new ArrayList<>();

        return null;
    }
}
